package com.gem.netty.message;

import com.gem.entity.Game;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.factory.user.UserFactory;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.service.common.IsCurrentUserRoundService;
import com.gem.netty.session.GroupSessionFactory;
import com.gem.pipeline.ProcessContext;
import com.gem.utils.R;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.List;

/**
 * Author: Xhy
 * CreateTime: 2022-09-25 17:07
 * 跳过该回合
 */
public class SkipRoundMessage implements Message{


    @Override
    public void handler(ClientContent content) {
        ChannelHandlerContext ctx = content.getCtx();
        Game game = GameFactory.get(content.getRoomId());
        String roomId = game.getRoomId();
        List<Channel> channels = GroupSessionFactory.getGroupSession().getMembersChannel(content.getRoomId());
        // 是否是当前用户回合
        IsCurrentUserRoundService service = new IsCurrentUserRoundService();
        ProcessContext<ClientContent> context = new ProcessContext<>();
        context.setNeedBreak(false);
        context.setData(content);
        service.process(context);
        if (context.getNeedBreak()){
            ctx.writeAndFlush(new TextWebSocketFrame(context.getResult().toString()));
            return;
        }
        String userName = UserFactory.get(content.getSendId());
//        setUserRound(game);
        // 告知其他用户该用户跳过该回合
        GameFactory.notifyUsers(channels,R.ok().data("color", MessageColorType.SYSTEM_MESSAGE.type).message(userName +" 跳过该回合").type(content.getType()),roomId);
        // 当前用户回合结束，到下一个用户
        GameFactory.roundStage(roomId);
    }


}
